package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.R;
import com.itheima.common.TokenHolder;
import com.itheima.domain.AddressBook;
import com.itheima.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

        /**
     * 查询默认地址
     */
    @GetMapping("default")
    public R<AddressBook> getDefault() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, TokenHolder.getCurrentId())
                    .eq(AddressBook::getIsDefault, 1);

        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        if (addressBook==null) {
            return R.error("没有找到该对象");
        } else {
            return R.success(addressBook);
        }
    }

        /**
         * 新增地址
         */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {
            addressBook.setUserId(TokenHolder.getCurrentId());
            log.info("addressBook:{}", addressBook);
            addressBookService.save(addressBook);
            return R.success(addressBook);
        }

    /**
     * 设置为默认地址
     */
    @PutMapping("/default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        log.info("addressBook:{}", addressBook);
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId, TokenHolder.getCurrentId());
        wrapper.set(AddressBook::getIsDefault, 0);
        //SQL:update address_book set is_default = 0 where user_id = ?
        addressBookService.update(wrapper);


        addressBook.setIsDefault(1);
      //  AddressBook addressBook1 = addressBookService.getOne(wrapper);
      //  addressBook1.setIsDefault("1");
        addressBookService.updateById(addressBook);
        return R.success(addressBook);


    }


    /**
     * 查询指定用户的全部地址
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list( ) {
        //条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq( AddressBook::getUserId, TokenHolder.getCurrentId())
                    .orderByDesc(AddressBook::getUpdateTime);

        return R.success(addressBookService.list(queryWrapper));
    }

    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    public R get(@PathVariable String id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return R.success(addressBook);
        } else {
            return R.error("没有找到该对象");
        }
    }

    /**
     * 删除对应id的地址
     * @param ids
     * @return
     */
    //todo:如果返回的是个code应该怎么写
    @DeleteMapping
    public  R<String> deleteOne(String ids){
        log.info("------删除菜品:{}-------",ids);
        addressBookService.delete(ids);
        return R.success("删除成功");
    }

    /**
     * 修改对应的地址信息
     * @param addressBook
     * @return
     */
    //todo:需要去看看updateById的sql语句
    //todo:突然发现update之前的逻辑上问题,因为更新的话需要保留未修改的片段,这就需要将两者都遍历,难道要一边get 一边set吗..
    //todo:这里偷个懒,直接删除再加入.
    @PutMapping
    public  R<String> update(@RequestBody AddressBook addressBook){
        boolean result = addressBookService.updateOne(addressBook);
        if(result){
            return  R.success("修改成功");
        }
        return R.error("修改失败");
    }
}
